# -*- coding: utf-8 -*-
'''
Module name: STREET_HATCHCSVImport

Created on 2013-3-20

@author: yangyil
'''
from SrcReader import SrcReader
from DATAImport import DATAImport
import string

class STREET_HATCHCSVImport(DATAImport):
    '''
    The main class of StrInfo importing.
    '''
    fields = "(TABLE_NO NUMBER(5),KEY_ID INT,AREA_CLS2_CODE SMALLINT,AREA_CLS3_CODE SMALLINT,AREA_CLS4_CODE SMALLINT,AREA_CLS5_CODE INT,LAN_CD SMALLINT,THIS_CHAR VARCHAR2(120),NUM NUMBER(10),NEXT_CHAR_BIT VARCHAR2(120))"
    charDic = {eval(rec.split('\t')[0]):rec.split('\t')[1].strip('\n') for rec in open('charDic').readlines()}
    
    def _ReadOneRow(self):
        '''
        Read one row  from the source file.
        '''
        row = []
        row.append(string.atoi(self._curFilePath[-6:-4]))
        for _ in range(6):
            temp = self._srcReader.ReadBy(',')
            if temp == '':
                row.append(temp)
            else:
                row.append(string.atoi(temp))
        row.append(self._srcReader.ReadBy(',').strip('"'))
        row.append(string.atoi(self._srcReader.ReadBy(',')))
        buff = self._srcReader.Read(34)
        buff = buff[1:]
        buff = buff.replace('""','"')
        row.append(self.GetStrFromBin(buff))
        self._srcReader.Read(33-len(buff))
        temp = self._srcReader.Read(2)
        assert temp == '\r\n', 'wrong line feed code'

        return row

    @staticmethod
    def GetStrFromBin(binBuff):
        '''
        '''
        binnum = SrcReader.getBinNumber(binBuff[:12])
        nums = []
        for n in range(1,96+1):
            if 2**(96-n)&binnum:
                nums.append(n-1)
        return ''.join([STREET_HATCHCSVImport.charDic[num] for num in nums])